<?php
/**
 * 上传(封装了上传接口)
 * @author liudaiming
 */
class Image{
	private $_api = array(
		'url' => 'http://api.iu1.xywy.com/upload.php',    //api接口地址 勿改
        'sign' => '96sga0khgof3dyaansdu' , // 请求码 用户身份验证和对应的项目目录识别
	);
	private $_error;
	
	public function upload(){
		$uploadedFile = array_shift($_FILES);
		$pathinfo = pathinfo($uploadedFile['name']);
		$f = fopen($uploadedFile['tmp_name'],'r');
        $line=fread($f, filesize($uploadedFile['tmp_name']));
        $post_str=array('file'=>$line,'sign'=>$this->_api['sign'],'suffix'=>$pathinfo['extension'],'path'=>$this->_api['path']);
        fclose($f);
        
        $ch = curl_init();
        //var_dump($ch);
        $ret = curl_setopt($ch, CURLOPT_URL, $this->_api['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str );
        $ret = curl_exec($ch);
        $ret_arr = json_decode($ret,true);
        if( curl_errno($ch) )
        {
            $this->_error = 'error'.curl_errno($ch).':'.curl_error($ch);
            return false;
        }else
        {
            if( $ret_arr['status']!=2 )
            {
                $this->_error = "接口出现错误";
                return false;
            }
            else
            {
                $temp_url = $temp_file_name = '';
                $temp_arr=array();
                $temp_arr = explode('/',$ret_arr['data']);
                $temp_file_name = array_pop($temp_arr);
                $temp_url = implode('/',$temp_arr);
                return $ret_arr['data'];
            }
        }
	}
	/**
	 * 图片批量上传
	 */
	public function uploadbatch($files,$sign){
		$imgurl_arr = array();
		if(!empty($files['name'][0])){
			$count = count($files['name']);
			for($i=0;$i<$count;$i++){
				$pathinfo = pathinfo($files['name'][$i]);
				$f = fopen($files['tmp_name'][$i],'r');
		        $line=fread($f,$files['size'][$i]);
		        $post_str=array('file'=>$line,'sign'=>$sign,'suffix'=>$pathinfo['extension'],'path'=>$this->_api['path']);
		        fclose($f);
		        $ch = curl_init();
		        //var_dump($ch);
		        $ret = curl_setopt($ch, CURLOPT_URL, $this->_api['url']);
		        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		        curl_setopt($ch, CURLOPT_POST, true);
		        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str );
		        $ret = curl_exec($ch);
		        $ret_arr = json_decode($ret,true);
		        if( curl_errno($ch) )
		        {
		            $this->_error = 'error'.curl_errno($ch).':'.curl_error($ch);
		            return false;
		        }else
		        {
		            if( $ret_arr['status']!=2 )
		            {
		                $this->_error = "接口出现错误";
		                return false;
		            }
		            else
		            {
		            	array_push($imgurl_arr,$ret_arr['data']);
		            }
		        }
			}
		}
		return  $imgurl_arr;
	}
	/**
	 * 单个文件上传
	 */
	public function uploadsingle($file,$sign){
		if(!empty($file['name'])){
			$pathinfo = pathinfo($file['name']);
			$f = fopen($file['tmp_name'],'r');
	        $line=fread($f,$file['size']);
	        $post_str=array('file'=>$line,'sign'=>$sign,'suffix'=>$pathinfo['extension'],'path'=>$this->_api['path']);
	        fclose($f);
	        $ch = curl_init();
	        $ret = curl_setopt($ch, CURLOPT_URL, $this->_api['url']);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_POST, true);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str );
	        $ret = curl_exec($ch);
	        $ret_arr = json_decode($ret,true);
	        if( curl_errno($ch) )
	        {
	            $this->_error = 'error'.curl_errno($ch).':'.curl_error($ch);
	            return false;
	        }else
	        {
	            if( $ret_arr['status']!=2 )
	            {
	                $this->_error = "接口出现错误";
	                return false;
	            }
	            else
	            {
	            	return $ret_arr['data'];
	            }
	        }
		}
		return '';	
			
	}
	public function testthumb($file,$sign){
		if(!empty($file['name'])){
			$pathinfo = pathinfo($file['name']);
			$f = fopen($file['tmp_name'],'r');
	        $line=fread($f,$file['size']);
	        $post_str=array('file'=>$line,'sign'=>$sign,'suffix'=>$pathinfo['extension'],'path'=>$this->_api['path'],'thumb'=>true);
	        fclose($f);
	        $ch = curl_init();
	        $ret = curl_setopt($ch, CURLOPT_URL, $this->_api['url']);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_POST, true);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str );
	        $ret = curl_exec($ch);
	        $ret_arr = json_decode($ret,true);
	        if( curl_errno($ch) )
	        {
	            $this->_error = 'error'.curl_errno($ch).':'.curl_error($ch);
	            return false;
	        }else
	        {
	            if( $ret_arr['status']!=2 )
	            {
	                $this->_error = "接口出现错误";
	                return false;
	            }
	            else
	            {
	            	return $ret_arr['data'];
	            }
	        }
		}
		return '';	
			
	}
	public function getError(){
		return $this->_error;
	}
}